/*
* Copyright (c) 2011
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
*
* Contributors:
*
* Description:
*
*/

#include <QWidget>
#include <math.h>
#include "WirelessSearch.h"
#include "WirelessRemote.h"

WirelessSearch::WirelessSearch(WirelessRemote *parent)
    : QDialog(parent)
{

    CLEAR_STR = "Clear";
    DELETE_STR = "Delete";
    SPACE_STR = "Space";

    mainDlg = parent;
    ui.setupUi(this);

    // Roku starts on A (1,1)
    setUpKeyboardMap();

    this->pos.col = 1;
    this->pos.row = 1;

}

WirelessSearch::~WirelessSearch()
{

}

void WirelessSearch::setUpKeyboardMap(){

    // Clear button
    pos.col=1; pos.row = 0;
    posMap[CLEAR_STR] = pos;

    // Delete button
    pos.col=2; pos.row = 0;
    posMap[DELETE_STR] = pos;

    // Space button
    pos.col=3; pos.row = 0;
    posMap[SPACE_STR] = pos;

    // Row 1
    pos.col = 1; pos.row = 1;
    posMap["A"] = pos;

    pos.col = 2; pos.row = 1;
    posMap["B"] = pos;

    pos.col = 3; pos.row = 1;
    posMap["C"] = pos;

    pos.col = 4; pos.row = 1;
    posMap["D"] = pos;

    pos.col = 5; pos.row = 1;
    posMap["E"] = pos;

    pos.col = 6; pos.row = 1;
    posMap["F"] = pos;

    // Row 2
    pos.col = 1; pos.row = 2;
    posMap["G"] = pos;

    pos.col = 2; pos.row = 2;
    posMap["H"] = pos;

    pos.col = 3; pos.row = 2;
    posMap["I"] = pos;

    pos.col = 4; pos.row = 2;
    posMap["J"] = pos;

    pos.col = 5; pos.row = 2;
    posMap["K"] = pos;

    pos.col = 6; pos.row = 2;
    posMap["L"] = pos;

    // Row 3
    pos.col = 1; pos.row = 3;
    posMap["M"] = pos;

    pos.col = 2; pos.row = 3;
    posMap["N"] = pos;

    pos.col = 3; pos.row = 3;
    posMap["O"] = pos;

    pos.col = 4; pos.row = 3;
    posMap["P"] = pos;

    pos.col = 5; pos.row = 3;
    posMap["Q"] = pos;

    pos.col = 6; pos.row = 3;
    posMap["R"] = pos;

    // Row 4
    pos.col = 1; pos.row = 4;
    posMap["S"] = pos;

    pos.col = 2; pos.row = 4;
    posMap["T"] = pos;

    pos.col = 3; pos.row = 4;
    posMap["U"] = pos;

    pos.col = 4; pos.row = 4;
    posMap["V"] = pos;

    pos.col = 5; pos.row = 4;
    posMap["W"] = pos;

    pos.col = 6; pos.row = 4;
    posMap["X"] = pos;

    // Row 5
    pos.col = 1; pos.row = 5;
    posMap["Y"] = pos;

    pos.col = 2; pos.row = 5;
    posMap["Z"] = pos;

    pos.col = 3; pos.row = 5;
    posMap["1"] = pos;

    pos.col = 4; pos.row = 5;
    posMap["2"] = pos;

    pos.col = 5; pos.row = 5;
    posMap["3"] = pos;

    pos.col = 6; pos.row = 5;
    posMap["4"] = pos;

    // Row 6
    pos.col = 1; pos.row = 6;
    posMap["5"] = pos;

    pos.col = 2; pos.row = 6;
    posMap["6"] = pos;

    pos.col = 3; pos.row = 6;
    posMap["7"] = pos;

    pos.col = 4; pos.row = 6;
    posMap["8"] = pos;

    pos.col = 5; pos.row = 6;
    posMap["9"] = pos;

    pos.col = 6; pos.row = 6;
    posMap["0"] = pos;

}

void WirelessSearch::on_backButton_clicked()
{
   this->close();
}

void WirelessSearch::on_aBut_clicked()
{
    calculatePositionAndDoCommand("A");
}

void WirelessSearch::handleSpecialKeyPress(QString letter){
    Position dest = getLocationOfPressedLetter(letter);
    QString command;

    if (dest.row == pos.row){
        // on same row, so just move left or right accordingly and press select
        int colDelta = dest.col - pos.col;
        if (colDelta > 0){
            for (int i = 0; i < colDelta; i++){
                command += "\npress right\n";
            }
        }
        else if (colDelta < 0) {
           int abs = qAbs(colDelta);
           for (int i = 0; i < abs; i++){
              command += "\npress left\n";
           }
        }
    } else {
        // moving from a regular key to a special key
        // First go far left
        for (int i = 0; i < 5; i++){
           command += "\npress left\n";
        }

        // Then go up
        for (int i = 0; i < pos.row; i++){
           command += "\npress up\n";
        }

        // finally go over
        if (letter.compare(DELETE_STR) == 0){
            command += "\npress right\n";
        } else if (letter.compare(SPACE_STR) == 0){
            command += "\npress right\n";
            command += "\npress right\n";
        }
    }

    command += "\npress select\n";

    // Set new postion root for next search
    pos.row = dest.row;
    pos.col = dest.col;

    mainDlg->sendRokuCommand(command);
}

void WirelessSearch::calculatePositionAndDoCommand(QString letter){

    if (letter.compare(CLEAR_STR) == 0 ||
        letter.compare(DELETE_STR) == 0 ||
        letter.compare(SPACE_STR) == 0){

         return handleSpecialKeyPress(letter); // clear, delete, space
    }

     QString command;
    if (pos.row == 0){
        // It's a special key, so just go left 3 times and down one to reset back to A
        // as exiting a special key can be odd how Roku remember (or doesn't) the last position.
        command += "\npress left\n";
        command += "\npress left\n";
        command += "\npress down";
        pos.col = 1;
        pos.row = 1;

    }


    Position dest = getLocationOfPressedLetter(letter);

    int rowDelta = dest.row - pos.row;
    int colDelta = dest.col - pos.col;

    // for each + col delta add a "press right" (less one)
    // for each - col delta add a "press left" (less one)
    // for each + row delta add a press down" (less one)
    // for each - row delta add a press up (less one)

    // left/right
    if (colDelta > 0){
        for (int i = 0; i < colDelta; i++){
            command += "\npress right\n";
        }
    }
    else if (colDelta < 0) {
       int abs = qAbs(colDelta);
       for (int i = 0; i < abs; i++){
          command += "\npress left\n";
       }
    }

    // up/down
    if (rowDelta > 0){
        for (int i = 0; i < rowDelta; i++){
            command += "\npress down\n";
        }
    }
    else if (rowDelta < 0) {
       int abs = qAbs(rowDelta);
       for (int i = 0; i < abs; i++){
          command += "\npress up\n";
       }
    }

    command += "\npress select\n";

    mainDlg->sendRokuCommand(command);

    // Set new postion root for next search
    pos.row = dest.row;
    pos.col = dest.col;

    // Update the edit box, except for clear, delete, space
    appendTextToSearch(letter);


}

void WirelessSearch::on_bBut_clicked()
{
    calculatePositionAndDoCommand("B");
}


void WirelessSearch::appendTextToSearch(QString string){
    QString searchStr = ui.searchEdit->toPlainText();
    searchStr += string;
    ui.searchEdit->setText(searchStr);
}

void WirelessSearch::on_cBut_clicked()
{
    calculatePositionAndDoCommand("C");
}

void WirelessSearch::on_dBut_clicked()
{
    calculatePositionAndDoCommand("D");
}

void WirelessSearch::on_eBut_clicked()
{
    calculatePositionAndDoCommand("E");
}

void WirelessSearch::on_fBut_clicked()
{
    calculatePositionAndDoCommand("F");
}

void WirelessSearch::on_spaceButton_clicked()
{
     appendTextToSearch(" ");
     calculatePositionAndDoCommand(SPACE_STR);
}

void WirelessSearch::on_deleteButton_clicked()
{
    QString searchStr = ui.searchEdit->toPlainText();
    if (searchStr.length() > 0){
        searchStr = searchStr.left(searchStr.length()-1);
        ui.searchEdit->setText(searchStr);
    }
    calculatePositionAndDoCommand(DELETE_STR);
}

void WirelessSearch::on_clearButton_clicked()
{
    ui.searchEdit->clear();
    calculatePositionAndDoCommand(CLEAR_STR);
}

Position WirelessSearch::getLocationOfPressedLetter(QString letter){
    QMultiMap<QString, Position>::iterator it = posMap.find(letter);
    return it.value();
}

void WirelessSearch::on_gBut_clicked()
{
     calculatePositionAndDoCommand("G");
}

void WirelessSearch::on_hBut_clicked()
{
     calculatePositionAndDoCommand("H");
}

void WirelessSearch::on_iBut_clicked()
{
     calculatePositionAndDoCommand("I");
}

void WirelessSearch::on_jBut_clicked()
{
     calculatePositionAndDoCommand("J");
}

void WirelessSearch::on_kBut_clicked()
{
     calculatePositionAndDoCommand("K");
}

void WirelessSearch::on_lBut_clicked()
{
     calculatePositionAndDoCommand("L");
}

void WirelessSearch::on_mBut_clicked()
{
    calculatePositionAndDoCommand("M");
}

void WirelessSearch::on_nBut_clicked()
{
    calculatePositionAndDoCommand("N");
}

void WirelessSearch::on_oBut_clicked()
{
    calculatePositionAndDoCommand("O");
}

void WirelessSearch::on_pBut_clicked()
{
    calculatePositionAndDoCommand("P");
}

void WirelessSearch::on_qBut_clicked()
{
    calculatePositionAndDoCommand("Q");
}


void WirelessSearch::on_rBut_clicked()
{
    calculatePositionAndDoCommand("R");
}

void WirelessSearch::on_sBut_clicked()
{
    calculatePositionAndDoCommand("S");
}

void WirelessSearch::on_tBut_clicked()
{
    calculatePositionAndDoCommand("T");
}

void WirelessSearch::on_uBut_clicked()
{
    calculatePositionAndDoCommand("U");
}

void WirelessSearch::on_vBut_clicked()
{
    calculatePositionAndDoCommand("V");
}

void WirelessSearch::on_wBut_clicked()
{
    calculatePositionAndDoCommand("W");
}

void WirelessSearch::on_xBut_clicked()
{
    calculatePositionAndDoCommand("X");
}

void WirelessSearch::on_yBut_clicked()
{
    calculatePositionAndDoCommand("Y");
}

void WirelessSearch::on_zBut_clicked()
{
    calculatePositionAndDoCommand("Z");
}

void WirelessSearch::on_oneBut_clicked()
{
    calculatePositionAndDoCommand("1");
}

void WirelessSearch::on_twoBut_clicked()
{
    calculatePositionAndDoCommand("2");
}

void WirelessSearch::on_threeBut_clicked()
{
    calculatePositionAndDoCommand("3");
}

void WirelessSearch::on_fourBut_clicked()
{
    calculatePositionAndDoCommand("4");
}

void WirelessSearch::on_fiveBut_clicked()
{
    calculatePositionAndDoCommand("5");
}

void WirelessSearch::on_sixBut_clicked()
{
    calculatePositionAndDoCommand("6");
}

void WirelessSearch::on_severBut_clicked()
{
    calculatePositionAndDoCommand("7");
}

void WirelessSearch::on_eightBut_clicked()
{
    calculatePositionAndDoCommand("8");
}

void WirelessSearch::on_nineBut_clicked()
{
    calculatePositionAndDoCommand("9");
}

void WirelessSearch::on_zeroBut_clicked()
{
    calculatePositionAndDoCommand("0");
}

void WirelessSearch::resizeEvent(QResizeEvent* event){
    QRect rect = this->geometry();
    if (rect.height() > rect.width()){
        // portrait
       // ui.setupUi(this);
       ui.searchEdit->setGeometry(QRect(10, 10, 340, 64));
    } else {
        // landscape
        //eBut->setGeometry(QRect(241, 142, 60, 60));
         //fBut->setGeometry(QRect(301, 142, 60, 60));
         ui.searchEdit->setGeometry(QRect(10, 10, 630, 64));
//         ui.gBut->setGeometry(QRect(361, 142, 60, 60));
//         ui.hBut->setGeometry(QRect(421, 142, 60, 60));
//         ui.iBut->setGeometry(QRect(481, 142, 60, 60));
//         ui.jBut->setGeometry(QRect(541, 142, 60, 60));
//         ui.hBut->setGeometry(QRect(601, 142, 60, 60));
    }
     QDialog::resizeEvent(event);
}
